<?php

/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright 2011
 * @license    All rights reserved
 **/
class photoSrv {

    /**
     * Fonction permettant d'uploader une image sur le serveur
     * @param array filename
     * @return boolean true if success
     *
     */
    public static function resizeImage ($file,$width=0,$height=0,$path=0) {
        if($width  == 0) $width  = PHOTO_ANNONCE_MINIATURE_WIDTH ;
        if($height == 0) $height = PHOTO_ANNONCE_MINIATURE_HEIGHT ;
        if($path   == 0) $path   = PHOTO_ANNONCE_MINIATURE_PATH ;
        $fileName = "";
        jClasses::inc('commun~uploadSrv');
        //print_r($file);die();
        
        $handle = new uploadSrv($file);
        try {
            if ($handle->uploaded) {
               // die('atreto');
                $handle->image_resize          = true;
                $handle->image_ratio_crop      = true;
                $handle->image_y               = $height;
                $handle->image_x               = $width;

                $handle->Process($path);

                // we check if everything went OK
                if ($handle->processed) {
                    $fileName = $handle->file_dst_name;
                }
                $handle-> Clean();
            } else {
                die    ('mgebona');
            }
        } catch (exception $e) {
            die($e->getMessage());
        }

        return $fileName;
    }

     /**
     * Fonction permettant de récuperer les photos d'une annonce à parit d'un objet annonce
     * 
     * @param array $annonce
     * @return array
     *
     */
    public static function getAnnoncePhoto ($annonce) {
        jClasses::inc('annonce~annonceSrv');
        jClasses::inc('commun~toolsFile');

        $photo_princ = "";
        $photo = $photoSrc = $media = array();

        //tester si le repertoire existe pour les données des anciennes annonces /datas/annonces/
        $trouve  = false;
        $dirPath = JELIX_APP_WWW_PATH . PATH_ANNONCE_IMG . $annonce->IdOccasion;
        if (is_dir($dirPath)) {
            //lister les images dans le repertoire
            if ($dh = opendir($dirPath)) {
                while (($file = readdir($dh)) !== false) {
                    $filename = $dirPath . '/'. $file;
                    $ext = explode(".", $file);
                    if (file_exists($filename) && isset($ext[1]) && toolsFile::is_image($ext[1])) {
                        // formation de l'objet photo
                        $images = new stdClass();
                        $images->photo_name = PATH_ANNONCE_IMG . $annonce->IdOccasion . '/' . $file;
                        $photo[] = $images;
                        $imgPath = "";
                        $trouve  = true;
                        break;
                    }
                }
                closedir($dh);
            }
        }
        // photo ppale la 1ère photo trouvée
        if($trouve) {
            $annonce->photo_name = $photo[0]->photo_name;
        }
        if(!$trouve && $annonce->IdOccasion){
            $photoSrc = annonceSrv::getPhotoAnnonce($annonce->IdOccasion, 1);
        }
        if(sizeof($photoSrc) > 0){
            $photo = array_merge($photo, $photoSrc);
        }
        if(($photo_princ == '') && (sizeof($photoSrc) > 0)){
            $annonce->photo_name = $photoSrc[0]->photo_name;
        }

        return $annonce;
    }

}